package pl.edu.agh.to1.dice.logic;

import java.util.*;
import java.io.*;
public class DiceGame {

	// Lista graczy
	private List<Player> players;

	// Wejscie
	private InputStreamReader converter;
	private BufferedReader in;

	// Liczba rund gry
	private int round;
	
	// Kostki do gry
	private Dices dices;

	public void play() {
		// inicjalizacja zmiennych
		players = new ArrayList<Player>();
		round = 0;
		int play = 0;
		dices = new Dices();
		converter = new InputStreamReader(System.in);
		in = new BufferedReader(converter);
		
		StringBuilder strBuilder = new StringBuilder("");
		strBuilder.append("\n##################################");
		strBuilder.append("\nadd [name] - dodanie gracza");
		strBuilder.append("\nplay - rozpoczecie rozgrywki");
		strBuilder.append("\nquit - wyjscie");
		System.out.println("Playing Dice");

		try
		{
			String line="";
			do{
				System.out.println(strBuilder.toString());
				line = in.readLine();
				String tmp[] = line.split(" ");
				if(line.equals("play")){
					play = 1;				
				}
				if(tmp[0].equals("add")){
					if(tmp.length > 1){
						if(addPlayer(new Player(tmp[1]))!=0)
							System.out.println("Nie dodano gracza");				
						else
							System.out.println("Dodano gracza");
					}
				}	
			} while(! (new String("quit").equals(line)) && play == 0);
		}
		catch(IOException e){
			System.out.println(e);
		}	
		if(play == 1 && players.size() > 0)
		{
			beginPlay();
		}
		else
			System.out.println("Nie spelniono wymagan do rozgrywki");
		// TODO
	}

	private int parse(String s){
		if(new String("1").equals(s))
			return 0;
		if(new String("2").equals(s))
			return 1;
		if(new String("3").equals(s))
			return 2;
		if(new String("4").equals(s))
			return 3;
		if(new String("5").equals(s))
			return 4;
		if(new String("6").equals(s))
			return 5;
		if(new String("3ki").equals(s))
			return 6;
		if(new String("4ki").equals(s))
			return 7;
		if(new String("ful").equals(s))
			return 8;
		if(new String("ms").equals(s))
			return 9;
		if(new String("ds").equals(s))
			return 10;
		if(new String("g").equals(s))
			return 11;
		if(new String("sz").equals(s))
			return 12;
		if(new String("l1").equals(s))
			return 13;
		if(new String("l2").equals(s))
			return 14;
		if(new String("l3").equals(s))
			return 15;
		if(new String("l4").equals(s))
			return 16;
		if(new String("l5").equals(s))
			return 17;
		if(new String("losuj").equals(s))
			return 18;
		return -1;
	}

	private void beginPlay(){
		try{
			StringBuilder strBuilder = new StringBuilder("");
			strBuilder.append("\n##################################");
			strBuilder.append("\n1,2,3,4,5,6,3ki,4ki,ful,ms,ds,g,sz - wybor miejsca do wspisania");
			strBuilder.append("\nl[num]- blokada kostki z num");
			strBuilder.append("\nlosuj - dokonanie nowego losowania");
			for(;round < 13;round++){
				System.out.println("Round "+(round+1));
				for(Player p : players){
					// wyswietlenie wyników i następnego gracza
					presentResult();
					System.out.print("Turn: "+p.toString());
					boolean cont = true;
					String line = "";

					// wyzerowanie kostek
					dices.kasuj();
					dices.losuj();
					int ile = 1;
					System.out.println(strBuilder.toString()); // wyswietlenie pomocy

					while(cont){
						
						int rezult[] = dices.getStates();
						// wyswietlenie kostek
						for(int i=0;i<5;i++)
						{
							if(!dices.getLock(i))
								System.out.print(rezult[i]+" ");
							else
								System.out.print("["+rezult[i]+"] ");
						}
						System.out.println(" ");
						line = in.readLine();
						int res = parse(line);
						if(res == -2)
							cont = false;
						if(res >= 0 && res < 13){
							if(p.getResult().isFree(res)){ // zaaktualizowanie kostek
								p.getResult().updateResult(res,dices); cont=false;}
							else
								System.out.println("Wybrana pozycja jest zajeta");
						}// pozostała obsługa programu
						if(res >= 13 && res < 18){
							dices.changeLock(res-13);
						}
						if(ile < 3 && res == 18){
							dices.zeruj();
							dices.losuj();
							ile++;						
						}
					}	
				}		
			}
		}
		catch(IOException e){
			System.out.println(e);
		}	
		presentResult();
	}
	
   	private int addPlayer(Player gamer){
		if(players.size() < 2){
			players.add(gamer);
			return 0;
		}
		return -1;
	}

	private void presentResult(){

		StringBuilder strBuilder = new StringBuilder("");
		strBuilder.append("##################################");
		strBuilder.append("\n            #      01           02  ");
		strBuilder.append("\n-----------------------------------");
		int delta =new String("             ").length();
		for(int i=0; i < 13; i++){
			strBuilder.append("\n");
			String a = PlayerResult.getName(i);
			int len = delta - a.length();
			strBuilder.append(a);
			while(len > 1){
				strBuilder.append(" ");
				len--;
			}
			strBuilder.append("#");
			for(Player p : players){
				PlayerResult r = p.getResult();
				if(r.getResult(i)>=0)
					strBuilder.append("      ").append(r.getResult(i)).append("      ");		
				else
					strBuilder.append("      _      ");
			}
			if(i == 5){
				strBuilder.append("\n-----------------------------------");
				strBuilder.append("\nPremia      #");
				for(Player p : players){
					PlayerResult r = p.getResult();
					strBuilder.append("      ").append(r.getSum(-1)).append("      ");		
				}
				strBuilder.append("\nSuma        #");
				for(Player p : players){
					PlayerResult r = p.getResult();
					strBuilder.append("      ").append(r.getSum(0)).append("      ");		
				}
				strBuilder.append("\n-----------------------------------");
			}
			if(i == 12){
				strBuilder.append("\n-----------------------------------");
				strBuilder.append("\nSuma        #");
				for(Player p : players){
					PlayerResult r = p.getResult();
					strBuilder.append("      ").append(r.getSum(1)).append("      ");		
				}
			}
		}
		strBuilder.append("\n##################################\n");
		
		System.out.println(strBuilder.toString());
	}
}
